#pragma tabsize 0
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <left4dhooks>

public Plugin myinfo =
{
	name = "l4d2_iammo_shotguns_2",
	author = "豆瓣酱な",
	description = "当玩家使用(霰弹枪)开枪时，​​主武器弹匣(当前子弹数)增加1​​，同时​​备用弹药(总弹药数)减少1​​.",
	version = "1.0",
	url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
};


public void OnPluginStart()
{
	HookEvent("weapon_fire", Event_WeaponFire);
}

public void Event_WeaponFire(Event event, const char[] name, bool dontBroadcast)
{
    char weapon[64];
    int client = GetClientOfUserId(event.GetInt("userid"));
    
    // 基本验证：确保玩家有效、在游戏中、是CT队伍且存活
    if (client <= 0 || !IsClientInGame(client) || GetClientTeam(client) != 2 || !IsPlayerAlive(client))
        return;
    
    event.GetString("weapon", weapon, sizeof(weapon));
    
    // 只处理特定霰弹枪
    if (StrEqual(weapon, "pumpshotgun") || StrEqual(weapon, "shotgun_chrome") ||
        StrEqual(weapon, "autoshotgun") || StrEqual(weapon, "shotgun_spas"))
    {
        int slot = 0; // 主武器槽位
        
        // 1. 先检查备用弹药是否大于0
        int ammoType = GetEntProp(GetPlayerWeaponSlot(client, slot), Prop_Send, "m_iPrimaryAmmoType");
        int currentAmmo = GetEntProp(client, Prop_Send, "m_iAmmo", _, ammoType);
        
        if (currentAmmo > 0) // 只有备用弹药>0时才执行后续操作
        {
            // 2. 增加当前弹匣中的子弹数(+1)
            int currentClip = GetEntProp(GetPlayerWeaponSlot(client, slot), Prop_Send, "m_iClip1");
            SetEntProp(GetPlayerWeaponSlot(client, slot), Prop_Send, "m_iClip1", currentClip + 1);
            
            // 3. 减少备用弹药(-1)
            SetEntProp(client, Prop_Send, "m_iAmmo", currentAmmo - 1, _, ammoType);
        }
        // 如果备用弹药=0，则什么都不做(不增加主弹匣子弹)
    }
}